package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.system.util.UserFeedbackStatusEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.UserAccountInfoDao;
import com.frogs.ucenter.dao.UserFeedbackDao;
import com.frogs.ucenter.dao.WechatUserBaseInfoDao;
import com.frogs.ucenter.entity.UserAccountInfo;
import com.frogs.ucenter.entity.UserFeedback;
import com.frogs.ucenter.entity.WechatUserBaseInfo;
import com.frogs.ucenter.v1.request.WechatUserComplaintRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.service.WechatUserComplaintService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION+"wechatUserComplaintService")
public class WechatUserComplaintServiceImpl extends BaseServiceImpl implements WechatUserComplaintService{

	@Resource
	UserFeedbackDao userFeedbackDao;
	
	@Resource
	WechatUserBaseInfoDao wechatUserBaseInfoDao;
	
	@Resource
	UserAccountInfoDao userAccountInfoDao;
	
    private static final Logger log = LoggerFactory.getLogger(WechatUserComplaintServiceImpl.class);
	
	@Override
	public Response insertByUserId(Request request) throws Exception {
		AddResponse<UserFeedback> response = new AddResponse<UserFeedback>();
		try {
			WechatUserComplaintRequest wucRequest = (WechatUserComplaintRequest)request;
			String openId = wucRequest.getOpenId();
			Integer openPlatformId = wucRequest.getOpenPlatformId();
			String content = wucRequest.getContent();
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("eqOpenId", openId);
			params.put("eqWechatApplicationId", openPlatformId);
			WechatUserBaseInfo wechatUserBaseInfo = new WechatUserBaseInfo();
			wechatUserBaseInfo = wechatUserBaseInfoDao.selectByMap(params, false).get(0);
			Integer userId = wechatUserBaseInfo.getUserId();
			Integer sexId = wechatUserBaseInfo.getSex();
			String nickName = wechatUserBaseInfo.getNickName();
			UserAccountInfo userAccountInfo = new UserAccountInfo();
			params.clear();
			params.put("eqId", userId);
			userAccountInfo = userAccountInfoDao.selectByMap(params, false).get(0);
			String email = userAccountInfo.getEmail();
			String phone = userAccountInfo.getPhone(); 
			UserFeedback userFeedback = new UserFeedback();
			userFeedback.setUserId(userId);
			userFeedback.setContent(content);
			userFeedback.setUserName(nickName);
			userFeedback.setSex(sexId);
			userFeedback.setStatus(UserFeedbackStatusEnum.HANDLING.getId());
			if(!email.equals("")){
				userFeedback.setEmail(email);
			}
			if(!phone.equals("")){
				userFeedback.setPhone(phone);
			}
			userFeedbackDao.insert(userFeedback);
			response.setInfo(userFeedback);
			response.setTip("投诉内容添加成功");
			
		} catch (Exception e) {
	    	log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

}
